import { defineStore } from "pinia";
import { Storage } from "@/utils/storage";
import { scene, sceneHelpers } from "@/components/viewport";
import * as THREE from "three";

const useEditorStore = defineStore("editor", {
  state: () => ({
    storage: Storage(), //修改数据存储
    helpers: [] as any[],
    geometries: [] as any[],
    materials: [] as any[],
    modelArr: [] as any[],
    videoArr: [] as any[],
    audioArr: [] as any[],
    imageArr: [] as any[],
    subscribeActive: true, //是否激活订阅
  }),
  getters: {
    //计算属性
    meshObjects: (state) => [
      ...state.modelArr,
      ...state.videoArr,
      ...state.imageArr,
    ], //存放需要添加辅助器的对象
    allObjects: (state) => [
      ...state.modelArr,
      ...state.videoArr,
      ...state.imageArr,
      ...state.helpers,
    ],
  },
  actions: {
    clear() {
      this.storage.clear();
      scene.children.forEach((obj: any) => {
        this.removeObject(obj);
      });
    },
    addObject(object: any) {
      let scope = this;
      object.traverse((child: any) => {
        if (child.geometry !== undefined) scope.addGeometry(child.geometry);
        if (child.material !== undefined) scope.addMaterial(child.material);
        scope.addHelper(child);
      });
      try {
        scene.add(object);
      } catch (error) {
        console.error("Error adding object to scene:", error);
      }
      console.log("addObject", object.type, this.meshObjects);
    },
    removeObject(object: any) {
      if (object.parent === null) return; // avoid deleting the camera or scene
      let scope = this;
      object.traverse((child: any) => {
        scope.removeHelper(child);
      });
      object.parent.remove(object);
      console.log("removeObject", object);
    },
    addHelper(object?: any) {
      let helper;
      if (object instanceof THREE.Camera) {
        helper = new THREE.CameraHelper(object, 1);
      } else if (object instanceof THREE.PointLight) {
        helper = new THREE.PointLightHelper(object, 1);
      } else if (object instanceof THREE.DirectionalLight) {
        helper = new THREE.DirectionalLightHelper(object, 1);
      } else if (object instanceof THREE.SpotLight) {
        helper = new THREE.SpotLightHelper(object, 1);
      } else if (object instanceof THREE.HemisphereLight) {
        helper = new THREE.HemisphereLightHelper(object, 1);
      } else if (object instanceof THREE.SkinnedMesh) {
        helper = new THREE.SkeletonHelper(object);
      } else {
        // no helper for this object type
        return;
      }
      this.helpers[object.id] = helper;
      sceneHelpers.add(helper);
    },
    removeHelper(object: any) {
      if (this.helpers[object.id] !== undefined) {
        var helper = this.helpers[object.id];
        helper.parent.remove(helper);

        delete this.helpers[object.id];
      }
    },
    addGeometry(geometry: any) {
      this.geometries[geometry.uuid] = geometry;
    },
    addMaterial(material: any) {
      this.materials[material.uuid] = material;
    },
    fromJSON(json: any) {
      // 初始化编辑器状态
      var loader = new THREE.ObjectLoader();
      console.log("fromJSON", json);
      if (!json || JSON.stringify(json?.scene) == "{}") return;
      let scene = loader.parse(json.scene);
      // 初始化场景
      console.log(scene.children);
      this.subscribeActive = false;
      while (scene.children.length > 0) {
        this.addObject(scene.children[0]);
      }
      this.subscribeActive = true;
    },
    toJSON() {
      return {
        scene: scene.toJSON() ? scene.toJSON() : {},
        // scene: {},
      };
    },
  },
});

export default useEditorStore();
